package ai4games_tests.soccer;

import game2dai.World;
import game2dai.entities.BaseEntity;
import game2dai.fsm.Dispatcher;
import game2dai.fsm.State;
import game2dai.fsm.Telegram;

public class TeamPrepareForKickOff extends State implements Constants{
	

	private static TeamPrepareForKickOff instance = null;
	
	public static TeamPrepareForKickOff state(){
		if(instance == null){
			instance = new TeamPrepareForKickOff();
			instance.name = "Prepare for kickoff";
		}
		return instance;
	}

	/** Implementing singleton pattern */
	private TeamPrepareForKickOff(){}
	
	@Override
	public void enter(BaseEntity owner) {
		Team team = (Team) owner;
		team.setControllingPlayer(null);
		team.setPlayerClosestToBall(null);
		team.setReceivingPlayer(null);
		team.setSupportingPlayer(null);
		team.nbrReachedTarget = 0;
		for(PlayerBase p : team.players){
			Dispatcher.dispatch(0, team.ID(), p.ID(), PLAYER_PREPARE_FOR_KICKOFF);
		}

	}

	@Override
	public void execute(BaseEntity owner, double deltaTime, World world) {
		Team team = (Team) owner;
		if(team.allPlayersReachedTarget()) {
			team.FSM().changeState(TeamDefending.state());
		}
	}

	@Override
	public void exit(BaseEntity owner) {
	}

	@Override
	public boolean onMessage(BaseEntity owner, Telegram tgram) {
		return false;
	}

}
